//
//  BaseViewController.m
//  sensen_ios
//
//  Created by mc on 16/3/21.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "BaseViewController.h"

@interface BaseViewController ()

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //操作输入框移动
    [self registerLJWKeyboardHandler];

    
    // Do any additional setup after loading the view.
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    // 设置默认导航栏样式
    [self isNaviTranslucent:NO statesLightContent:YES];
    
    // 是首页的三个控制器则显示标签栏
    if ([self.navigationController.viewControllers indexOfObject:self] == 0) {
        // 退出的时候要显示导航栏
        MainTabBarController *tabBarController = (MainTabBarController *)self.navigationController.tabBarController;
        [tabBarController showTabBar];
    }
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    if ([SVProgressHUD isVisible]) {
        [SVProgressHUD dismissAfterDelay:2];
    }
    
    [BoyeHttpClient cancelAllOperations];
}

/**
 *  导航栏样式设置，有透明和绿色两种
 *
 *  默认的样式是：白色状态栏，不透明导航栏
 *
 *  @param translucent 导航栏样式有透明和绿色两种
 *  @param light       状态栏颜色，YES则为白色，NO为黑色
 */
- (void)isNaviTranslucent:(BOOL)translucent statesLightContent:(BOOL)light {
    
    // 导航栏标题白色
    NSDictionary *attr = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:B_FONT(20)};
    [self.navigationController.navigationBar setTitleTextAttributes:attr];
    
    // 导航栏样式设置
    if (translucent) {
        if (!light) {
            [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
        } else {
            [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
        }
        self.navigationController.navigationBar.translucent = YES;
        self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor]] forBarMetrics:UIBarMetricsDefault];
        self.navigationController.navigationBar.shadowImage = [UIImage imageWithColor:[UIColor clearColor]];
    } else {
        if (!light) {
            [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
        } else {
            [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
        }
        self.navigationController.navigationBar.translucent = NO;
        self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:SS_NAVI_BLUE_COLOR] forBarMetrics:UIBarMetricsDefault];
        self.navigationController.navigationBar.shadowImage = [UIImage imageWithColor:[UIColor clearColor]];
    }
}

#pragma mark - 导航栏左边返回按钮
// 左侧按钮的样式可以自定义，由imgname控制，如果填nil则默认是返回样式
- (void)createLeftBarButtonItemWithImagename:(NSString *)imgname {
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25/2.0, 43/2.0)];
    [leftButton addTarget:self action:@selector(leftAction:) forControlEvents:UIControlEventTouchUpInside];
    // 如果穿的是nil，则是默认的返回样式按钮
    if (imgname == nil) {
        [leftButton setImage:[UIImage imageNamed:@"导航栏_返回"] forState:UIControlStateNormal];
    } else {
        [leftButton setImage:[UIImage imageNamed:imgname] forState:UIControlStateNormal];
    }
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftBarButton;
}

// 右侧按钮的样式可以自定义，由imgname控制，如果是文字则调用下面的接口
- (void)createRightBarButtonItemWithImagename:(NSString *)imgname {
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25/2.0, 44/2.0)];
    [rightButton addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchUpInside];
    [rightButton setImage:[UIImage imageNamed:imgname] forState:UIControlStateNormal];
    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = rightBarButton;
}

// 创建导航栏右侧的按钮，样式是文字的
- (void)createRightBarButtonItemWithTitle:(NSString *)title {
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
    [rightButton setTitle:title forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    rightButton.titleLabel.font = FONT(15);
    rightButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [rightButton addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = rightBarButton;
}


#pragma mark - 点击事件
// 导航栏左侧按钮事件
- (void)leftAction:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}
// 导航栏右侧按钮事件
- (void)rightAction:(UIButton *)button {
    
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
